#include <modules\graphics\camera.h>

namespace DelGE
{
	namespace Modules
	{
		namespace Graphics
		{
			Camera::Camera(int iX, int iY, int iWidth, int iHeight)
			{
				this->iViewportX = iX; this->iViewportY = iY;
				this->iViewportWidth = iWidth; this->iViewportHeight = iHeight;

				this->SetScale(1, 1, 1);
				this->SetPosition(0, 0, 0);
				this->SetRotation(0, 0, 0);

				this->oBackgroundColor = Color(112, 140, 255, 255);
			}

			Camera::~Camera()
			{
			}

			void Camera::SetOrthographicProjection(float fLeft, float fRight, float fTop, float fBottom, float fNear, float fFar)
			{
				this->oProjection = Matrix::CreateOrthographicsProjection(fLeft, fRight, fTop, fBottom, fNear, fFar);
			}
			
			void Camera::SetPerspectiveProjection(float fFieldOfView, float fAspect, float fNear, float fFar)
			{
				this->oProjection = Matrix::CreatePerspectiveProjection(fFieldOfView, fAspect, fNear, fFar);
			}

			void Camera::SetViewport(int iX, int iY, int iWidth, int iHeight)
			{
				this->iViewportX = iX; this->iViewportY = iY;
				this->iViewportWidth = iWidth; this->iViewportHeight = iHeight;
			}

			int Camera::GetViewportX()
			{
				return this->iViewportX;
			}

			int Camera::GetViewportY()
			{
				return this->iViewportY;
			}

			int Camera::GetViewportWidth()
			{
				return this->iViewportWidth;
			}

			int Camera::GetViewportHeight()
			{
				return this->iViewportHeight;
			}
			
			void Camera::SetBackgroundColor(const Color& rColor)
			{
				this->oBackgroundColor = rColor;
			}

			Color Camera::GetBackgroundColor()
			{
				return this->oBackgroundColor;
			}

			Vector3 Camera::Project2D(const Vector3& rVector, int iScreenWidth, int iScreenHeight)
			{
				Vector3 mouseRatio;

				mouseRatio.x = (rVector.x - this->GetViewportX()) / this->GetViewportWidth();
				mouseRatio.y = (rVector.y - this->GetViewportY()) / this->GetViewportHeight();

				return Vector3(mouseRatio.x * iScreenWidth, mouseRatio.y * iScreenHeight, 0);
			}

			Matrix Camera::GetProjectionMatrix()
			{
				return this->oProjection;
			}

			Frustum Camera::GetFrustum()
			{
				return Frustum::CreateFromProjectionMatix(this->GetProjectionMatrix() * (this->GetLocalMatrix().Inverse()));
			}
		}
	}
}